package com.jumper.study.front.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumper.study.db.dao.BannerMapper;
import com.jumper.study.db.model.Banner;
import com.jumper.study.front.dto.resp.BannerResp;
import com.jumper.study.front.service.BannerService;

@Service("frontBannerService")
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public List<BannerResp> homeList(int count) {
        if (count <= 0) {
            count = defaultCount;
        }

        LambdaQueryWrapper<Banner> query = Wrappers.lambdaQuery();
        query.select(Banner::getId, Banner::getTitle, Banner::getCover, Banner::getLink);
        query.eq(Banner::getOnself, true);
        query.orderByDesc(Banner::getId);
        query.last("limit " + count);

        List<Banner> banners = bannerMapper.selectList(query);
        return banners.stream().map(b -> {
            BannerResp resp = new BannerResp();
            resp.setTitle(b.getTitle());
            resp.setCover(b.getCover());
            resp.setLink(b.getLink());
            return resp;
        }).toList();
    }
    
}
